package com.transformer.shobal.activitys.wigets;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.BounceInterpolator;
import com.nineoldandroids.animation.IntEvaluator;
import com.nineoldandroids.animation.ValueAnimator;
import com.transformer.shobal.R;

import java.lang.ref.WeakReference;

/**
 * Created by Administrator on 2015/2/2.
 */
public class PullDownRotationView extends View{
    WeakReference<Context> mContextHolder;
    private float mLastY;
    private int minHeight;
    private Bitmap bitmapNp;
    private NinePatch np;
    private double rotationDegree;

    public PullDownRotationView(Context context) {
        super(context);
        mContextHolder = new WeakReference<Context>(context);
        init();
    }

    public PullDownRotationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContextHolder = new WeakReference<Context>(context);
        init();
    }

    public PullDownRotationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContextHolder = new WeakReference<Context>(context);
        init();
    }

    private void init(){
        minHeight = 400;
        setImageResId(R.drawable.star_swing_10);
    }

    public void setImageResId(int resId){
        bitmapNp = BitmapFactory.decodeResource(mContextHolder.get().getResources(),resId);
        np = new NinePatch(bitmapNp,bitmapNp.getNinePatchChunk(),null);
        Log.i("shobal","--------init getHeight()="+getHeight()+",getWidth()="+getWidth());
    }

    public int getMinHeight() {
        return minHeight;
    }

    public void setMinHeight(int minHeight) {
        this.minHeight = minHeight;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i("shobal","--------onDraw bitmapNp.getWidth()="+bitmapNp.getWidth()+",bitmapNp.getHeight()="+bitmapNp.getHeight());
        Log.i("shobal","--------onDraw getHeight()="+getHeight()+",getWidth()="+getWidth());
        canvas.drawColor(Color.WHITE);
        Paint paint = new Paint();
        paint.setColor(Color.GREEN);
        canvas.drawRect(getWidth()/2-bitmapNp.getWidth()/2,getHeight()/4,getWidth()/2+bitmapNp.getWidth()/2,getHeight()*3/4,paint);
        paint.setColor(Color.RED);
        canvas.drawCircle(getWidth()/2,0,10,paint);
        if(bitmapNp !=null ){
            canvas.rotate((float)rotationDegree,getWidth()/2,getHeight()/2);
//            getLayoutParams().width = bitmapNp.getWidth();
        }
        if(np !=null ){
            np.draw(canvas,new Rect(getWidth()/2-bitmapNp.getWidth()/2,getHeight()/2,getWidth()/2+bitmapNp.getWidth()/2,getHeight()));
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("shobal", "onTouchEvent " +",event.getX()="+event.getX()+",+event.getY()"+event.getY());
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mLastY = event.getY();
                return  true;
            case MotionEvent.ACTION_MOVE:
                //拉伸
                int height = (int)(event.getY()-mLastY);
                mLastY = event.getY();
                getLayoutParams().height += height;
                if(getLayoutParams().height < minHeight)
                    getLayoutParams().height = minHeight;

                //旋转
                /*double a = 0;
                double b = 0;
                double c = 0;
                double degree = 0;

                //余玄定理
                b = c = getHeight();
                a = Math.sqrt(Math.pow((event.getX()-getWidth()/2),2)+Math.pow(event.getY()-getHeight(),2));
                double cosA = ((Math.pow(b,2)+Math.pow(c,2)) - (Math.pow((event.getX()-getWidth()/2),2)+Math.pow(event.getY()-getHeight(),2))) / (2*b*c);

                if(event.getX() < getWidth()/2 ){
                    degree =Math.acos(cosA)*180;
                }else if(event.getX() > getWidth()/2 ){
                    degree =Math.acos(cosA)*(-180);
                }if(event.getX() == getWidth()/2){
                    degree = 0;
                }
                if(degree == 0)
                    break;
                Log.i("shobal", "onTouchEvent MotionEvent.ACTION_MOVE degree=" + degree+",event.getX()="+event.getX()+",+event.getY()"+event.getY()+",getWidth()/2="+getWidth()/2 + ",a=" + a + ",b=" + b + ",c=" + c + ",cosA=" + cosA);
                */
                double a = 0;
                if(event.getX() <=0 ){
                    a = getWidth()/2 + Math.abs(event.getX());
                }else{
                    a = getWidth()/2 - event.getX();
                }
                if(a == 0)
                    break ;
                double c = Math.sqrt(Math.pow(a,2)+Math.pow(getY(),2));
                rotationDegree = Math.asin(a/c)*180;
                Log.i("shobal","onTouchEvent rotationDegree="+rotationDegree+",a="+a+",c="+c+",getWidth()/2="+getWidth()/2+",event.getX()="+event.getX()+",+event.getY()"+event.getY());
                requestLayout();
                break;
            case MotionEvent.ACTION_UP:
                //弹回
                mLastY = getLayoutParams().height;
                ValueAnimator v = ValueAnimator.ofInt(1,100);
                v.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                    //持有一个IntEvaluator对象，方便下面估值的时候使用
                    private IntEvaluator mEvaluator = new IntEvaluator();

                    @Override
                    public void onAnimationUpdate(ValueAnimator animator) {
                        //获得当前动画的进度值，整型，1-100之间
                        int currentValue = (Integer) animator.getAnimatedValue();

                        //计算当前进度占整个动画过程的比例，浮点型，0-1之间
                        float fraction = currentValue / 100f;

                        //这里我偷懒了，不过有现成的干吗不用呢
                        //直接调用整型估值器通过比例计算出宽度，然后再设给Button
                        getLayoutParams().height = mEvaluator.evaluate(fraction, (int)mLastY, minHeight);
                        requestLayout();
                    }
                });
                v.setInterpolator(new BounceInterpolator());
                v.setDuration(1000);
                v.start();
                break;
        }
        return super.onTouchEvent(event);
    }
}
